<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Login extends CI_Controller {

    protected  $userDb;
    protected  $base_url;

    function __construct() {
        parent::__construct();
        $this->load->model('user');
        $this->base_url = $this->config->config['base_url'];
    }

	public function index()
	{
        $data['base_url'] = $this->base_url;
		$this->load->view('admin/login', $data);
	}

    public function loginin()
    {
        $username = isset($_POST['username']) && strlen($_POST['username']) ? trim($_POST['username']) : null;
        $password = isset($_POST['password']) && strlen($_POST['password']) ? trim($_POST['password']) : null;

        if($username === null || $password === null){
            echo '<script>alert("账户名和密码不能为空");window.location.href="'.$this->base_url.'/index.php/admin/login";</script>';
        }
        $user = $this->user->get_user(array('username' => $username));
        $retrun = false;
        if($user){
            if(md5($password) == $user['password']){
                $retrun = true;
            }
        }

        if($retrun){
            session_start();
            $_SESSION['user_id'] = $user['uid'];
            $_SESSION['username'] = $user['username'];
            header('location:' .$this->base_url.'/index.php/admin/index');
        }else{
            echo '<script>alert("登录失败");window.location.href="'.$this->base_url.'/index.php/admin/login";</script>';
        }
    }

    public function loginout()
    {   session_start();
        unset($_SESSION['user_id']);
        unset($_SESSION['username']);
        header('location:'.$this->base_url.'/index.php/admin/login/index');
    }
}
